package javafx.demo;

import java.io.File;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class TestChoiceFile extends Application {

	private BorderPane outerRoot;
	private Label label;
	private Text text;
	private Button btn;
	private File file;

	@Override
	public void start(Stage primaryStage) {
		label = new Label("提示信息：");
		text = new Text("测试");
		btn = new Button();
		btn.setText("Say 'Hello World'");
		btn.setOnAction(setText("hello world!"));

		Label pathLabel = new Label("选择文件信息：");
		Text pathText = new Text("路径");
		Button pathBtn = new Button();
		pathBtn.setText("Open FileChooser");
		pathBtn.setOnAction(event -> {
			FileChooser fileChooser = new FileChooser();
			fileChooser.setTitle("请选择目录");
			// Set extension filter
			FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
					"ALL files (*)", "*");
			fileChooser.getExtensionFilters().add(extFilter);

			// Show open file dialog
			file = fileChooser.showOpenDialog(null);

			pathText.setText(file.getPath());
		});
		
		Label dirLabel = new Label("选择路径：");
		Text dirText = new Text("路径");
		Button dirBtn = new Button();
		dirBtn.setText("Open PathChooser");
		
		dirBtn.setOnAction(event -> {
			DirectoryChooser directoryChooser = new DirectoryChooser();
			directoryChooser.setTitle("请选择文件");

			// Show open file dialog
			file = directoryChooser.showDialog(null);

			dirText.setText(file.getPath());
		});

		VBox root = new VBox();
		root.setAlignment(Pos.BASELINE_CENTER); // 对其方式，居中
		root.setSpacing(5);
		ObservableList<Node> rootList = root.getChildren();
		rootList.add(label);
		rootList.add(text);
		rootList.add(btn);

		rootList.add(pathLabel);
		rootList.add(pathText);
		rootList.add(pathBtn);

		rootList.add(dirLabel);
		rootList.add(dirText);
		rootList.add(dirBtn);
		
		outerRoot = new BorderPane();
		outerRoot.setTop(getTop());
		outerRoot.setCenter(root);

		outerRoot.setBottom(getBottom());

		Scene scene = new Scene(outerRoot, 500, 450);

		primaryStage.setTitle("Hello World!");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

	/**
	 * 底部信息
	 * 
	 * @return
	 */
	private HBox getBottom() {
		// 底部
		Button exitBtn = new Button("退出");
		exitBtn.setOnAction(event -> {
			Platform.exit();
		});
		HBox bottom = new HBox();
		bottom.setSpacing(5);
		bottom.setAlignment(Pos.BOTTOM_LEFT);
		bottom.getChildren().add(exitBtn);
		return bottom;
	}

	private EventHandler<ActionEvent> setText(final String content) {
		EventHandler<ActionEvent> helloWordBtnAction = event -> {
			showText(content);
		};
		return helloWordBtnAction;
	}

	private void showText(final String content) {
		System.out.println(content);
		text.setText(content);
	}

	/**
	 * 获取顶部信息
	 * 
	 * @return
	 */
	private MenuBar getTop() {
		MenuBar menuBar = new MenuBar();
		menuBar.setUseSystemMenuBar(true);

		ToggleGroup fontTg = new ToggleGroup();
		Menu fontSizeMenu = new Menu("Font");

		fontSizeMenu.getItems().addAll(
				buildFontRadioMenuItem("System Default", null, fontTg),
				buildFontRadioMenuItem("Mac", null, fontTg),
				buildMenuItemSeparator(fontTg),
				buildFontRadioMenuItem("Windows 100%", null, fontTg),
				buildFontRadioMenuItem("Windows 125%", null, fontTg),
				buildFontRadioMenuItem("Windows 150%", null, fontTg),
				buildMenuItemSeparator(fontTg),
				buildFontRadioMenuItem("Linux", null, fontTg),
				buildFontRadioMenuItem("Embedded Touch", null, fontTg),
				buildFontRadioMenuItem("Embedded Small", null, fontTg));
		Menu sizeMenu = new Menu("Size");

		ToggleGroup sizeTg = new ToggleGroup();
		sizeMenu.getItems().addAll(buildFontRadioMenuItem("9px", null, sizeTg),
				buildFontRadioMenuItem("13px", null, sizeTg),
				buildFontRadioMenuItem("15px", null, sizeTg),
				buildFontRadioMenuItem("18px", null, sizeTg),
				buildFontRadioMenuItem("22px", null, sizeTg));
		menuBar.getMenus().add(fontSizeMenu);
		menuBar.getMenus().add(sizeMenu);

		return menuBar;
	}

	private RadioMenuItem buildMenuItemSeparator(ToggleGroup fontTg) {
		RadioMenuItem radioMenu = new RadioMenuItem("------------", null);
		radioMenu.setDisable(true);
		radioMenu.setToggleGroup(fontTg);
		return radioMenu;
	}

	public RadioMenuItem buildFontRadioMenuItem(String name, final Node node,
			ToggleGroup tg) {
		RadioMenuItem radioMenu = new RadioMenuItem(name, node);
		radioMenu.setOnAction(setText(name));
		radioMenu.setToggleGroup(tg);
		return radioMenu;
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String[] args) {
		launch(args);
	}

}
